package com.zrrd.jt_admin.common.config;

import com.zrrd.jt_admin.common.intercept.LoginInterceptor;
import com.zrrd.jt_admin.common.intercept.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author WanJl
 * @version 1.0
 * @title WebMvcConfig
 * @description SpringMvc的核心配置类
 * @create 2024/7/2
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    //创建自定义的拦截器对象

    /**
     * 创建Token拦截器对象
     *
     * @return
     */
    @Bean //把这个拦截器对象作为Bean放到IOC容器
    public TokenInterceptor getTokenInterceptor() {
        return new TokenInterceptor();
    }

    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }


    //向springMVC添加自定义的拦截器对象
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTokenInterceptor())
                .addPathPatterns("/**")//拦截所有请求
                .excludePathPatterns(   //放行的请求url
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**",
                        "/swagger-resources/**",
                        "/api",
                        "/api-docs",
                        "/api-docs/**",
                        "/doc.html/**","/**/login");
        registry.addInterceptor(getLoginInterceptor())
                .addPathPatterns("/**/login");
    }
}
